#include <stdio.h>
#include "../Format.h"
#include "../YamlValidator.h"
#include "../Utils.h"

int main(int argc, char** argv, char** env) {

    unsigned succeeded = 0, total = 7;
    
    ValidityError ve = syvNoError;
    
    for(int index = 0; index < 5; index++) {    
        try {
            char s[64];
            sprintf(s, "test/cyclic%u.yml", index);
            printf("Running test \"%s\"...", s);
            FormatValidator fv(YAML::LoadFile(s));
        }
        catch(ValidityException e) {
            ve = e.error();
//            printf("Exception caught: %s\n", e.what());
        }
        catch(std::exception e) {
            printf("Unforseen exception thrown: %s.\n", e.what());
        }
        if(ve == syvCyclicReferenceDetected) {
            succeeded++;
            printf("OK\n");
        }
    }

    ve = syvNoError;

    try {
        char s[64] = "test/validation.yml";
        printf("Running test \"%s\" #1...", s);
        FormatValidator fv(YAML::LoadFile(s));
    }
    catch(ValidityException e) {
        ve = e.error();
        printf("Exception caught: %s\n", e.what());
    }
    catch(std::exception e) {
        printf("Unforseen exception thrown: %s.\n", e.what());
    }
    if(ve == syvNoError) {
        succeeded++;
        printf("OK\n");
    }

    ve = syvNoError;

    try {
        char s[64] = "test/validation.yml";
        printf("Running test \"%s\" #2...", s);
        FormatValidator fv(YAML::LoadFile(s));
        YamlValidator yv(fv, YAML::LoadFile("test/test.yml"));
        YAML::Node n = yv.getValue("structtest");
        YAML::Node comp = YAML::Load("[ [ { q: { p: { ieks: x }, d: [ { x: [ [ 66, xx ], [ 21, 34 ], [ 123456789012, 123456789012 ] ], v: { v: { z: 6, y: , x: 0.123, w: 1 }, c: { r: 1, g: 0.321, b: .0, a: 0.321 } }, type:  }, { x: [ [ 123456789012, 123456789012 ], [ 123456789012, 123456789012 ], [ 123456789012, 123456789012 ] ], e: 2, v: { v: { z: .0, y: , x: 0.123, w: 1 }, c: { r: 1, g: 0.321, b: .0, a: 1 } }, type:  }, { x: [ [ 123456789012, 123456789012 ], [ 123456789012, 123456789012 ], [ 123456789012, 123456789012 ] ], e: 2, v: { v: { z: .0, y: , x: 0.123, w: 1 }, c: { r: 1, g: 0.321, b: .0, a: 1 } }, type:  } ] }, b: 6.66, prime: { s: 3 } }, { q: { p: { ieks: x }, d: [ { x: [ [ xy, xx ], [ 12, 34 ], [ 123456789012, 123456789012 ] ], v: { v: { z: 2, y: , x: 0.123, w: 1 }, c: { r: 1, g: 0.321, a: 0.321, b: .0 } }, type:  }, { x: [ [ 123456789012, 123456789012 ], [ 123456789012, 123456789012 ], [ 123456789012, 123456789012 ] ], e: 2, v: { v: { x: 0.123, z: .0, y: , w: 1 }, c: { r: 1, g: 0.321, b: .0, a: 1 } }, type:  }, { x: [ [ 123456789012, 123456789012 ], [ 123456789012, 123456789012 ], [ 123456789012, 123456789012 ] ], e: 2, v: { v: { x: 0.123, z: .0, y: , w: 1 }, c: { r: 1, g: 0.321, b: .0, a: 1 } }, type:  } ] }, b: -10, prime: { s: 5 } }, { q: { p: { ieks: x }, d: [ { x: [ [ xy, xx ], [ 12, 34 ], [ 123456789012, 123456789012 ] ], v: { v: { z: 2, y: , x: 0.123, w: 1 }, c: { r: 1, g: 0.321, a: 0.321, b: .0 } }, type:  }, { x: [ [ 123456789012, 123456789012 ], [ 123456789012, 123456789012 ], [ 123456789012, 123456789012 ] ], e: 2, v: { v: { x: 0.123, z: .0, y: , w: 1 }, c: { r: 1, g: 0.321, b: .0, a: 1 } }, type:  }, { x: [ [ 123456789012, 123456789012 ], [ 123456789012, 123456789012 ], [ 123456789012, 123456789012 ] ], e: 2, v: { v: { x: 0.123, z: .0, y: , w: 1 }, c: { r: 1, g: 0.321, b: .0, a: 1 } }, type:  } ] }, b: -11, prime: { s: 3 } } ] ]");
        if(comparesEqual(n, comp)) {
            succeeded++;
            printf("OK\n");
        }
    }

    catch(ValidityException e) {
        ve = e.error();
        printf("Exception caught: %s\n", e.what());
    }
    catch(std::exception e) {
        printf("Unforseen exception thrown: %s.\n", e.what());
    }
    
    printf("%u/%u succeeded\n", succeeded, total);
    
    return 0;
}
